package com.sunnypower.rushfitapp;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.sunnypower.rushfitapp.util.GetData;


public class RegActivity  extends AppCompatActivity implements View.OnClickListener{
    private EditText et_number;
    private EditText et_password;
    private EditText et_password2;
    private Button btn_back;
    private Button btn_submit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rushfit_reg);

        init();
        }

    private void init() {
        et_number = (EditText) findViewById(R.id.et_number);
        et_password = (EditText) findViewById(R.id.et_password);
        et_password2 = (EditText) findViewById(R.id.et_password2);
        btn_back = (Button) findViewById(R.id.bt_backlog);
        btn_submit = (Button) findViewById(R.id.bt_submitreg);
        //设置按钮的点击事件
        btn_back.setOnClickListener(this);
        btn_submit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String username;
        String password;
        String password2;
        switch (v.getId()) {
            case R.id.bt_backlog:
                finish();
                break;
            case R.id.bt_submitreg:
                username = et_number.getText().toString().trim();
                password = et_password.getText().toString().trim();
                password2 = et_password2.getText().toString().trim();
                if(TextUtils.isEmpty(username)|| TextUtils.isEmpty(password)||TextUtils.isEmpty(password2)){
                    Toast.makeText(this, "请输入完整的注册信息", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!password.equals(password2)){
                    Toast.makeText(this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
                    return;
                }
                //注册
                String data= GetData.doGet("http://120.24.24.141:10990/rushfit/reg2app?username="+username+"&password="+password);
                System.out.println(data);
                if(data.equals("success")){
                    Toast.makeText(this,"注册成功",Toast.LENGTH_SHORT).show();
                    finish();
                }else{
                    Toast.makeText(this,"该账号已被注册",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}
